<?php
namespace app\home\controller;

use app\home\common\Common;
use app\home\common\Page;
use app\home\common\Pagetwo;
use app\infrastructure\uri\Request;
use app\infrastructure\uri\Uri;

/**
 * Created by PhpStorm.
 * User: YD
 * Date: 2017/4/26
 * Time: 16:52
 */
class Product extends Common
{
    public function productList()
    {
        $type=$_GET['type'];
        if($type == 'list'){
            $p=empty(input('p'))?1:input('p');
            $startIndex=($p-1)*10;
            $countUrl=Uri::HOST.Uri::PRODUCTLIST;
            $url=Uri::HOST.Uri::PRODUCTLIST."?limitation=10&offset=".$startIndex;
            $client=new Request();
            $value = $client->getArrayData($url);
            $totalCount =$client->getArrayData($countUrl);
            if($value['code'] != 0){
                $value['data']=[];
            }
            $pager=new Page(count($totalCount['data']),10,$p);
            $page=$pager->show();
            $this->assign('page',$page);
            $this->assign('data',$value['data']);
            $this->assign('totalCount',count($totalCount['data']));
            return view();
        }else if($type == 'info'){
             $this->productInfo();
            return view('productInfo');
        }

    }
    public function productAdd(){
        if(\think\Request::instance()->isAjax()){
            $preg='/\/+/';
            if(preg_match($preg,$_POST['name'])){
                return ['code'=>2,'msg'=>'0x400','data'=>"the product name is invalid!"];
            }
            if(empty($_POST['sort'])){
                return ['code'=>2,'msg'=>'0x400','data'=>"the product type is invalid!"];
            }
            if($_POST['sort'] == 100){
                $product_configuration=[];
                $totalRepayMoney=0;
              foreach($_POST['repay_period'] as $k=>$v){
                $product_configuration[]=[
                    "repay_period"=>$v,
                    "repay_order"=>$k+1,
                    "repay_money"=>$_POST['repay_money'][$k]
                ];
                  $totalRepayMoney += $_POST['repay_money'][$k];
              }
                $totalRepayMoney = $totalRepayMoney -$_POST['repay_money'][0];
                if($totalRepayMoney != $_POST['price']){
                    return ['code'=>2,'msg'=>'0x400','data'=>"the ewi every repay is not eq the total price!"];
                }
                $product_configuration=json_encode($product_configuration);
            }
            else{
                $product_configuration="";
            }
            $data=[
                'name'=>$_POST['name'],
                'admin_name'=>$_POST['admin_name'],
                'operator_id'=>session('userid'),
                'operator_name'=>session('username'),
                'sort'=>$_POST['sort'],
                'plantform'=>$_POST['plantform'],
                'comment'=>$_POST['comment'],
                'img_id'=>'',
                'price'=>$_POST['price'],
                'is_valid'=>$_POST['is_valid'],
                'product_rate'=>$_POST['product_rate'],
                'overdue_rate'=>$_POST['overdue_rate'],
                'share_url'=>$_POST['share_url'],
                'period'=>$_POST['period'],
                'info_check_fee'=>$_POST['info_check_fee'],
                'manage_fee'=>$_POST['manage_fee'],
                'convenience_fee'=>$_POST['convenience_fee'],
                'product_configuration'=>$product_configuration,
                'max_loan'=>$_POST['max_loan']
            ];
            $url=Uri::HOST.Uri::ADDPRODUCT;
            $client=new Request();
            $value = $client->getArrayData($url,$data,3);
           if($value["code"] == 0){
               if($_POST['sort'] == 100){
                   $product_id=$value['data']['id'];
                   $addProductEwiConfigUrl=Uri::HOST.Uri::ADDEWIPRODUCT;
                   $ewiData=[
                       "product_id"=>$product_id,
                       "repay_period"=>$_POST['repay_count'],
                       "repay_info"=>$product_configuration,
                       "operator_id"=>session('userid'),
                       'operator_name'=>session('username')
                   ];
                   $res=$client->getArrayData($addProductEwiConfigUrl,$ewiData,3);
               }
           }
            return $value;
        }
        return view();
    }
    public function upStore(){
        if(\think\Request::instance()->isAjax()){
            $data=[
                'is_valid'=>$_POST['is_valid']
            ];
            $id=$_GET['id'];
            $url=Uri::HOST.Uri::UPDATEPRODUCT."/".$id;
            $client=new Request();
            $value = $client->getArrayData($url,$data,4);
           return $value;
        }
    }
    public function productInfo(){
        $id=$_GET['uid'];
        $url=Uri::HOST.Uri::PRODUCTLIST."?id=".$id;
        $client=new Request();
        $value = $client->getArrayData($url);
        if($value['code'] == 0){
            $data=$value['data'][0];
            if($data['sort'] == 100){
                $data["product_configuration"]=json_decode($data["product_configuration"]);
            }
        }else{
            $data=[];
        }
        $this->assign('data',$data);
    }
}